/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.model;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.openide.util.NbBundle;

/**
 *
 * @author Gerben Feenstra
 */
@NamedQueries(value = {
    @NamedQuery(name = CropProtection.Q_FIND_ALL_CROPPROTECTION, query = "select cp from CropProtection cp"),
    @NamedQuery(name = CropProtection.Q_FIND_CROPPROTECTION_BY_NAME_AND_LASTUSAGE, query = "select cp from CropProtection cp where cp. name = :name and cp.lastUsage = :lastusage")
})
@Entity
@Table(name = "CROP_PROTECTION")
public class CropProtection extends Bean implements Serializable, Persistable {

    public static final String Q_FIND_ALL_CROPPROTECTION = "findAllCropProtections";
    public static final String Q_FIND_CROPPROTECTION_BY_NAME_AND_LASTUSAGE = "findCropProtectionByNameAndLastUsage";
        
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID")
    private Long id;
    
    // Toelatingsnummer
    @Column(name = "AUTHORIZATION_NUMBER")
    private String authorizationNumber;
    
    // Toelatingshouder / Bedrijfsnaam / Producent
    @Column(name = "AUTHORIZATION_HOLDER")
    private String authorizationHolder;
    
    // Werkzame stof
    @Column(name = "ACTIVE_SUBSTANCE")
    private String activeSubstance;
    
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "LAST_UPDATED")
    private Date lastUpdated;
    
    // Laatst toegestane gebruiksdatum
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "LAST_USAGE_DATE")
    private Date lastUsage;
    
    // Vervaldatum
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "EXPIRATION_DATE")
    private Date expiration;
    
    @Column(name = "W_CODE")
    private String wCode;
    
    @Column(name = "FORMULATION")
    private Formulation formulation;
    
    // Etiketteringszinnen
    @Column(name = "LABELLING")
    private String labelling;
    
    @Column(name = "DANGER_ICON")
    private DangerIcon dangerIcon;
    
    @Column(name = "CROP_GROUP")
    private String cropgroup;
    
    @Column(name = "CROP_NAME")
    private String cropname;
            
    @Column(name = "DOSAGE_MINIMAL")
    private String dosageMinimal;
    
    @Column(name = "DOSAGE_MAXIMAL_EACH_TIME")
    private String dosageMaximalEachTime;   
    
    @Column(name = "DOSAGE_MAXIMAL_PER_SEASON")
    private String dosageMaximalPerSeason; 

    @Column(name = "DOSAGE_UNIT")
    private DosageUnit dosageUnit;
    
    @Column(name = "CONCENTRATION_DOSAGE_UNIT")
    private ConcentrationDosageUnit concentrationDosageUnit;
  
    @Column(name = "APPLICATION_MAXIMUM_TOTAL")
    private Long applicationsMaximumTotal;
    
    @Column(name = "APPLICATION_MAXIMUM_IN_SEQUENCE")
    private Long applicationsMaximumInSequence;
    
    @Column(name = "APPLICATION_INTERVAL")
    private Long applicationInterval;
    
    @Column(name = "SAFETY_TERM")
    private Long safetyTerm;  
    
    // Aard van de werking
    @Column(name = "PROTECTION_TYPE")
    private CropProtectionType cropProtectionType; 

    @Override
    public long getId() {
        return id;
    }
    
    public String getwCode() {
        return wCode;
    }

    public void setwCode(String wCode) {
        this.wCode = wCode;
    }

    public CropProtectionType getCropProtectionType() {
        return cropProtectionType;
    }

    public void setNlCropProtectionType(CropProtectionType cropProtectionType) {
        CropProtectionType old = this.cropProtectionType;
        this.cropProtectionType = cropProtectionType;
        getPropertyChangeSupport().firePropertyChange("cropProtectionType", old, cropProtectionType);
        fireUndoableEvent("cropProtectionType", this, old, cropProtectionType);
        this.cropProtectionType = cropProtectionType;
    }

    public String getAuthorizationHolder() {
        return authorizationHolder;
    }

    public void setAuthorizationHolder(String authorizationHolder) {
        this.authorizationHolder = authorizationHolder;
    }

    public String getAuthorizationNumber() {
        return authorizationNumber;
    }

    public void setAuthorizationNumber(String authorizationNumber) {
        this.authorizationNumber = authorizationNumber;
    }

    public String getActiveSubstance() {
        return activeSubstance;
    }

    public void setActiveSubstance(String activeSubstance) {
        this.activeSubstance = activeSubstance;
    }

    public Date getLastUpdated() {
        return lastUpdated;
    }

    public void setLastUpdated(Date lastUpdated) {
        this.lastUpdated = lastUpdated;
    }

    public Date getLastUsage() {
        return lastUsage;
    }

    public void setLastUsage(Date lastUsage) {
        this.lastUsage = lastUsage;
    }

    public Long getApplicationInterval() {
        return applicationInterval;
    }

    public void setApplicationInterval(Long applicationInterval) {
        this.applicationInterval = applicationInterval;
    }

    public Long getApplicationsMaximumInSequence() {
        return applicationsMaximumInSequence;
    }

    public void setApplicationsMaximumInSequence(Long applicationsMaximumInSequence) {
        this.applicationsMaximumInSequence = applicationsMaximumInSequence;
    }

    public Long getApplicationsMaximumTotal() {
        return applicationsMaximumTotal;
    }

    public void setApplicationsMaximumTotal(Long applicationsMaximumTotal) {
        this.applicationsMaximumTotal = applicationsMaximumTotal;
    }

    public String getCropgroup() {
        return cropgroup;
    }

    public void setCropgroup(String cropgroup) {
        this.cropgroup = cropgroup;
    }

    public String getCropname() {
        return cropname;
    }

    public void setCropname(String cropname) {
        this.cropname = cropname;
    }

    public DangerIcon getDangerIcon() {
        return dangerIcon;
    }

    public void setDangerIcon(DangerIcon dangerIcon) {
        this.dangerIcon = dangerIcon;
    }

    public String getDosageMaximalEachTime() {
        return dosageMaximalEachTime;
    }

    public void setDosageMaximalEachTime(String dosageMaximalEachTime) {
        this.dosageMaximalEachTime = dosageMaximalEachTime;
    }

    public String getDosageMaximalPerSeason() {
        return dosageMaximalPerSeason;
    }

    public void setDosageMaximalPerSeason(String dosageMaximalPerSeason) {
        this.dosageMaximalPerSeason = dosageMaximalPerSeason;
    }

    public String getDosageMinimal() {
        return dosageMinimal;
    }

    public void setDosageMinimal(String dosageMinimal) {
        this.dosageMinimal = dosageMinimal;
    }

    public DosageUnit getDosageUnit() {
        return dosageUnit;
    }

    public void setDosageUnit(DosageUnit dosageUnit) {
        this.dosageUnit = dosageUnit;
    }

    public ConcentrationDosageUnit getConcentrationDosageUnit() {
        return concentrationDosageUnit;
    }

    public void setConcentrationDosageUnit(ConcentrationDosageUnit concentrationDosageUnit) {
        this.concentrationDosageUnit = concentrationDosageUnit;
    }
   
    public Date getExpiration() {
        return expiration;
    }

    public void setExpiration(Date expiration) {
        this.expiration = expiration;
    }

    public Formulation getFormulation() {
        return formulation;
    }

    public void setFormulation(Formulation formulation) {
        this.formulation = formulation;
    }

    public String getLabelling() {
        return labelling;
    }

    public void setLabelling(String labelling) {
        this.labelling = labelling;
    }

    public Long getSafetyTerm() {
        return safetyTerm;
    }

    public void setSafetyTerm(Long safetyTerm) {
        this.safetyTerm = safetyTerm;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        if(!super.equals(obj)){
            return false;
        }
        final CropProtection other = (CropProtection) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        if ((this.authorizationNumber == null) ? (other.authorizationNumber != null) : !this.authorizationNumber.equals(other.authorizationNumber)) {
            return false;
        }
        if ((this.authorizationHolder == null) ? (other.authorizationHolder != null) : !this.authorizationHolder.equals(other.authorizationHolder)) {
            return false;
        }
        if ((this.activeSubstance == null) ? (other.activeSubstance != null) : !this.activeSubstance.equals(other.activeSubstance)) {
            return false;
        }
        if (this.lastUpdated != other.lastUpdated && (this.lastUpdated == null || !this.lastUpdated.equals(other.lastUpdated))) {
            return false;
        }
        if (this.lastUsage != other.lastUsage && (this.lastUsage == null || !this.lastUsage.equals(other.lastUsage))) {
            return false;
        }
        if (this.expiration != other.expiration && (this.expiration == null || !this.expiration.equals(other.expiration))) {
            return false;
        }
        if ((this.wCode == null) ? (other.wCode != null) : !this.wCode.equals(other.wCode)) {
            return false;
        }
        if (this.formulation != other.formulation) {
            return false;
        }
        if ((this.labelling == null) ? (other.labelling != null) : !this.labelling.equals(other.labelling)) {
            return false;
        }
        if (this.dangerIcon != other.dangerIcon) {
            return false;
        }
        if ((this.cropgroup == null) ? (other.cropgroup != null) : !this.cropgroup.equals(other.cropgroup)) {
            return false;
        }
        if ((this.cropname == null) ? (other.cropname != null) : !this.cropname.equals(other.cropname)) {
            return false;
        }
        if ((this.dosageMinimal == null) ? (other.dosageMinimal != null) : !this.dosageMinimal.equals(other.dosageMinimal)) {
            return false;
        }
        if ((this.dosageMaximalEachTime == null) ? (other.dosageMaximalEachTime != null) : !this.dosageMaximalEachTime.equals(other.dosageMaximalEachTime)) {
            return false;
        }
        if ((this.dosageMaximalPerSeason == null) ? (other.dosageMaximalPerSeason != null) : !this.dosageMaximalPerSeason.equals(other.dosageMaximalPerSeason)) {
            return false;
        }
        if (this.dosageUnit != other.dosageUnit) {
            return false;
        }
        if (this.concentrationDosageUnit != other.concentrationDosageUnit) {
            return false;
        }
        if (this.applicationsMaximumTotal != other.applicationsMaximumTotal && (this.applicationsMaximumTotal == null || !this.applicationsMaximumTotal.equals(other.applicationsMaximumTotal))) {
            return false;
        }
        if (this.applicationsMaximumInSequence != other.applicationsMaximumInSequence && (this.applicationsMaximumInSequence == null || !this.applicationsMaximumInSequence.equals(other.applicationsMaximumInSequence))) {
            return false;
        }
        if (this.applicationInterval != other.applicationInterval && (this.applicationInterval == null || !this.applicationInterval.equals(other.applicationInterval))) {
            return false;
        }
        if (this.safetyTerm != other.safetyTerm && (this.safetyTerm == null || !this.safetyTerm.equals(other.safetyTerm))) {
            return false;
        }
        if (this.cropProtectionType != other.cropProtectionType) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = super.hashCode();
        hash = 89 * hash + (this.id != null ? this.id.hashCode() : 0);
        hash = 89 * hash + (this.authorizationNumber != null ? this.authorizationNumber.hashCode() : 0);
        hash = 89 * hash + (this.authorizationHolder != null ? this.authorizationHolder.hashCode() : 0);
        hash = 89 * hash + (this.activeSubstance != null ? this.activeSubstance.hashCode() : 0);
        hash = 89 * hash + (this.lastUpdated != null ? this.lastUpdated.hashCode() : 0);
        hash = 89 * hash + (this.lastUsage != null ? this.lastUsage.hashCode() : 0);
        hash = 89 * hash + (this.expiration != null ? this.expiration.hashCode() : 0);
        hash = 89 * hash + (this.wCode != null ? this.wCode.hashCode() : 0);
        hash = 89 * hash + (this.formulation != null ? this.formulation.hashCode() : 0);
        hash = 89 * hash + (this.labelling != null ? this.labelling.hashCode() : 0);
        hash = 89 * hash + (this.dangerIcon != null ? this.dangerIcon.hashCode() : 0);
        hash = 89 * hash + (this.cropgroup != null ? this.cropgroup.hashCode() : 0);
        hash = 89 * hash + (this.cropname != null ? this.cropname.hashCode() : 0);
        hash = 89 * hash + (this.dosageMinimal != null ? this.dosageMinimal.hashCode() : 0);
        hash = 89 * hash + (this.dosageMaximalEachTime != null ? this.dosageMaximalEachTime.hashCode() : 0);
        hash = 89 * hash + (this.dosageMaximalPerSeason != null ? this.dosageMaximalPerSeason.hashCode() : 0);
        hash = 89 * hash + (this.dosageUnit != null ? this.dosageUnit.hashCode() : 0);
        hash = 89 * hash + (this.concentrationDosageUnit != null ? this.concentrationDosageUnit.hashCode() : 0);
        hash = 89 * hash + (this.applicationsMaximumTotal != null ? this.applicationsMaximumTotal.hashCode() : 0);
        hash = 89 * hash + (this.applicationsMaximumInSequence != null ? this.applicationsMaximumInSequence.hashCode() : 0);
        hash = 89 * hash + (this.applicationInterval != null ? this.applicationInterval.hashCode() : 0);
        hash = 89 * hash + (this.safetyTerm != null ? this.safetyTerm.hashCode() : 0);
        hash = 89 * hash + (this.cropProtectionType != null ? this.cropProtectionType.hashCode() : 0);
        return hash;
    }

    

    public static class Builder {

        private CropProtection cropProtection;
        
        public Builder() {
            cropProtection = new CropProtection();
        }
        
        public Builder setName(String name){
            cropProtection.setName(name);
            return this;
        }
        
        public Builder setLastUsage(Date lastUsage){
            cropProtection.setLastUsage(lastUsage);
            return this;
        }
        
        public Builder setDosageUnit(DosageUnit dosageUnit){
            cropProtection.setDosageUnit(dosageUnit);
            return this;
        }
        
        public Builder setConcentrationDosageUnit(ConcentrationDosageUnit concentrationDosageUnit){
            cropProtection.setConcentrationDosageUnit(concentrationDosageUnit);
            return this;
        }
        
        public CropProtection build(){
            return cropProtection;
        }        
    }    
    
    public enum Formulation{
        LIQUID, POWDER;
        
        public String getName(){
            return NbBundle.getMessage(Formulation.class, "cropprotection.formulation." + this.name().toLowerCase());
        }
    }
    
    public enum DangerIcon{
        POISONOUS, IRRITATING, COROSSIVE, INFLAMABLE, EXPLODABLE, OXIDATING, ENVIRONMENTAL_RISK;
        
        public String getName(){
            return NbBundle.getMessage(DangerIcon.class, "cropprotection.dangericon." + this.name().toLowerCase());
        }
    }
}
